package com.bj58.ecdata.dashboard.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;

/** 
 * @author wangchonglu  2016年4月1日 
 *
 */
public class CateUtils {
	
	/**
	 *    从数据库查询单个字段的集合  （去重）
	* * * @param tableSuffix  表名后缀
	** * * @param cateName    字段名
	** * * @param condition   条件
	** * * @param service     AbstractBaseService
	** * * @param clazz       实体类
	** * * @return
	** * * @throws IllegalAccessException
	** * * @throws IllegalArgumentException
	** * * @throws InvocationTargetException
	 */
	public static <T> List<T> getCateByCateName(String tableSuffix,String cateName,String condition,AbstractBaseService<?> service,Class<?> clazz) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{	
		return getCateList(cateName, service.getListByConditon(tableSuffix,"DISTINCT "+cateName,condition, cateName),clazz);
		
	}
	
	@SuppressWarnings("unchecked")
	public static  <T> List<T>  getCateList(String column,List<?> list,Class<?> clazz) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
				
		List<T> return_list = new ArrayList<T>();
		String getter = "get"+column.substring(0, 1).toUpperCase()+column.substring(1);
		Method[] methods = clazz.getDeclaredMethods();
		Method method = null;
		for (Method method1 : methods) {
			 if(getter.equals(method1.getName())){	
				 method = method1;
				 break;
			 }
		}
		for (Object object : list) {
			
			 return_list.add((T) method.invoke(object));	
		}
		return return_list;	 
	}	
	
}

